package com.dish.action;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dish.bean.CartItemBean;

/**
 * Servlet implementation class ShowCart
 */
@WebServlet("/ShowCart")
public class ShowCart extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowCart() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		double sumPrice = 0;  //总价
		String fucku = "";
		HttpSession session = request.getSession(false);
		@SuppressWarnings("unchecked")
		Map<Integer, CartItemBean> cart = (Map<Integer, CartItemBean>) session.getAttribute("cart");
		if (cart == null) {
	          cart = new HashMap<Integer, CartItemBean>();
	          sumPrice = 0;
	    }else{
	    	for(Integer key:cart.keySet())
	    	{
//	    		//使用BigDecimal精确计算乘法
//                BigDecimal b1 = new BigDecimal(Double.toString(price));
//                BigDecimal b2 = new BigDecimal(etAmount.getText().toString());
//                //精度，保留两位小数
//                java.text.DecimalFormat myFormat=new java.text.DecimalFormat("0.00");
//                tvAll.setText( myFormat.format(b1.multiply(b2).doubleValue()) );
	    		//使用BigDecimal进行精确计算
	    		fucku = fucku + cart.get(key).getDish().getDishname();
	    		BigDecimal b1=new BigDecimal(Double.toString(cart.get(key).getQuantity()));  //数量
	    		BigDecimal b2=new BigDecimal(Double.toString(cart.get(key).getDish().getPrice()));  //单价
	    		BigDecimal b3=new BigDecimal(Double.toString(sumPrice));  //总价
	    		//精度，保留两位小数
	    		java.text.DecimalFormat myFormat=new java.text.DecimalFormat("0.00");
	    		sumPrice= Double.parseDouble(myFormat.format(b3.add(b1.multiply(b2))));
//	    		sumPrice = (double) (sumPrice+(cart.get(key).getQuantity()*cart.get(key).getDish().getPrice()));
	    	}
	    }
		session.setAttribute("sumPrice", sumPrice);
		session.setAttribute("fuck", fucku);
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("/showCart.jsp");
		requestDispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
